package licode.unisop.types.util;

import licode.unisop.types.dp.AusUrlItem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 操作属性相关对象
 *
 * @author WR
 * @since 2016/7/9 17:26
 */
public class AuPropUtil {
    public static void mergeUrlItems(List<AusUrlItem> urlItems) {
        if (null != urlItems) {
            Map<Integer, AusUrlItem> stateUrls = new HashMap<>();
            for (AusUrlItem item: urlItems) {
                AusUrlItem urlItem = stateUrls.get(item.getState());
                if (urlItem == null) {
                    urlItem = new AusUrlItem();
                    urlItem.setState(item.getState());
                    stateUrls.put(item.getState(), urlItem);
                }
                if (urlItem.getUrls() == null) {
                    urlItem.setUrls(new ArrayList<>());
                }
                urlItem.getUrls().addAll(item.getUrls());
            }

            urlItems.clear();
            urlItems.addAll(stateUrls.values());
            for (AusUrlItem item: urlItems) {
                item.setUrls(item.getUrls()
                        .stream().distinct().collect(Collectors.toList()));
            }
        }
    }
}
